"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserDao = void 0;
const Config_1 = require("../config/Config");
const DbConfig_1 = require("../config/DbConfig");
const DaoBase_1 = require("../engine/dao/DaoBase");
const Logger_1 = require("../engine/utils/Logger");
const MysqlManager_1 = require("../engine/utils/MysqlManager");
class UserDao extends DaoBase_1.DaoBase {
    static getDbName() {
        // if (Config.environment == Config.EnvironmentType.yueyouhui) {
        //     return DbConfig.Db_Name.db_yxx2
        // }
        // else if (Config.environment == Config.EnvironmentType.xj) {
        //     return DbConfig.Db_Name.db_yxx2
        // }
        // else if (Config.environment == Config.EnvironmentType.Jk) {
        //     return DbConfig.Db_Name.db_jk
        // }
        // else if(Config.environment == Config.EnvironmentType.chaoyou){
        //     return DbConfig.Db_Name.db_chaoyouhui
        // }
        // else {
        return DbConfig_1.DbConfig.Db_Name.db_yxx;
        // }
    }
    static get_user_count() {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            let sql = `select count(id) as count from usr`;
            let results = yield client.queryPromise(sql);
            let count = (results && results.length) ? results[0].count : 0;
            return count;
        });
    }
    static get_today_user_count() {
        return __awaiter(this, void 0, void 0, function* () {
            let nowDate = new Date();
            let startDate = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate());
            let endDate = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate() + 1);
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = `select count(id) as count from usr where createtime >= ? and createtime <= ?`;
            let sqlParams = [Math.floor(startDate.getTime() / 1000), Math.floor(endDate.getTime() / 1000)];
            let results = yield client.queryPromise(sql, sqlParams);
            let count = (results && results.length) ? results[0].count : 0;
            return count;
        });
    }
    static get_user_list(pagenum, pagesize) {
        return __awaiter(this, void 0, void 0, function* () {
            let dbName = this.getDbName();
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(dbName);
            let start = (pagenum - 1) * pagesize;
            let sql = "select  usr.id,usr.`name`,usr.nick,usr.face,usr.createtime, usr_resource.gem  from usr  left join usr_resource  on usr.id=usr_resource.id  limit ?,? ";
            let sqlParams = [start, pagesize];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("get_user_list=== sql=", dbName, Config_1.Config.environment, sql, sqlParams);
            return results;
        });
    }
    static is_user_exist_by_userid(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            let sql = "select userid from " + this.DB_NAME + "." + this.Tb_Name + " where `userid`=?;";
            let sqlParams = [userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results.length > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_user_data_by_userid(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            let sql = "select  usr.id,usr.`name`,usr.nick,usr.face,usr.createtime, usr_resource.gem  from usr  left join usr_resource   on usr.id=usr_resource.id  where usr.id=?";
            let sqlParams = [userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results.length > 0) {
                return results[0];
            }
        });
    }
    static get_user_data(account) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            let sql = "select * from " + this.DB_NAME + "." + this.Tb_Name + " where `account`=?;";
            let sqlParams = [account];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results.length > 0) {
                return results[0];
            }
        });
    }
}
exports.UserDao = UserDao;
UserDao.DB_NAME = DbConfig_1.DbConfig.Db_Name.db_yxx;
UserDao.Tb_Name = "usr";
